package com.qf.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qf.admin.entity.User;
import com.qf.admin.mapper.IUserMapper;
import com.qf.admin.service.IUserService2;
import com.qf.admin.model.TableData;
import com.qf.admin.pojo.QueryUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class UserServiceImpl2 implements IUserService2 {

    private IUserMapper userMapper;

    public UserServiceImpl2(IUserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public TableData<User> getPageData(QueryUser queryUser, Integer offset, Integer limit) {

        String name = queryUser.getName();
        if (null != name && !"".equals(name.trim())) {
            queryUser.setName("%" + name + "%");
        }

        String email = queryUser.getEmail();
        if (null != email && !"".equals(email.trim())){
            queryUser.setEmail("%" + email + "%");
        }


        Page<User> page = PageHelper.offsetPage(offset, limit);

        List<User> userList = userMapper.getPageDate(queryUser);

        TableData<User> tableData = new TableData<>();

        tableData.setRows(userList);
        tableData.setTotal(page.getTotal());
        return tableData;
    }

    @Override
    public void disableUser(Integer id) {
            userMapper.disableUser(id);

    }

    @Override
    public void activeUser(Integer id) {
        userMapper.activeUser(id);
    }
}
